﻿#region Copyright (c) 2000-2008 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{       AgDataGrid                                 }
{                                                                   }
{       Copyright (c) 2000-2008 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2008 Developer Express Inc.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Shapes;
using DevExpress.AgMenu.Internal;
namespace DevExpress.AgMenu {
	public class AgMenuLoader : List<AgMenuItem> {
		public AgMenuLoader() { }
		public AgMenuLoader(Stream stream) {
			LoadXML(stream);
		}
		AgMenuItem GetItem(MenuItem item) {
			AgMenuItem m = new AgMenuItem();
			m.Header = item.Content;
			m.IsCheckable = item.IsCheckable;
			m.IsChecked = item.IsChecked;
			m.IsRadioItem = item.IsRadioItem;
			if (item.Icon != null) {
				BitmapImage bi = new BitmapImage();
				bi.SetSource(new MemoryStream(item.Icon));
				m.Icon = new Image() { Source = bi };
			}
			foreach(MenuItem mi in item.Items) {
				m.Items.Add(GetItem(mi));
			}
			return m;
		}
		public void LoadXML(Stream stream) {
			foreach(MenuItem item in MenuItemSource.DataSource(stream)) {
				Add(GetItem(item));
			}
		}
		string uri;
		public string Source {
			get { return uri; }
			set {
				if(uri == value) return;
				uri = value;
				LoadXML(Application.GetResourceStream(new Uri(Source, UriKind.Relative)).Stream);
			}
		}
	}
}
namespace DevExpress.AgMenu.Internal {
	public class MenuItem {
		public MenuItem() {
			Items = new List<MenuItem>();
		}
		[XmlAttribute]
		public string Content { get; set; }
		public byte[] Icon { get; set; }
		[XmlAttribute]
		public bool IsCheckable { get; set; }
		[XmlAttribute]
		public bool IsChecked { get; set; }
		[XmlAttribute]
		public bool IsRadioItem { get; set; }
		public List<MenuItem> Items { get; set; }
	}
	[XmlRoot("Menu")]
	public class MenuItemSource : List<MenuItem> {
		public static MenuItemSource DataSource(Stream stream) {
			XmlSerializer s = new XmlSerializer(typeof(MenuItemSource));
			return s.Deserialize(stream) as MenuItemSource;
		}
	}
}
